package com.tianqu.tms.core.webservice;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.util.enums.WeChatResultCode;
import com.tianqu.tms.dto.MarketCarInfoDto;
import com.tianqu.tms.dto.TransferAcceptedInfoDto;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.entity.TransferAcceptedInfo;
import com.tianqu.tms.entity.TransferFinanceInfo;
import com.tianqu.tms.entity.TransferPersonalCarInfo;
import com.tianqu.tms.entity.TransferProcessHistory;


public class AbstractTmsCarWeChatService {
	
	private Logger LOGGER = Logger.getLogger(AbstractTmsCarWeChatService.class);
	/**
	 * @param Car
	 * @return int
	 */
	public int agencyCarRegister(Car inputCar) {
		
		Car car = Car.getInstance();
			
//		TransferAcceptedInfo Tai = TransferAcceptedInfo.getInstance();
		
		TxController tx = TxController.getTxController();
		
		int result = WeChatResultCode.SUCCESS.getValue();
		
		try{
			tx.begin();
			
			//车辆编号到库存车辆信息表中搜索
			/*boolean isExistSerial = car.isExistInMarketCarInfoBySerial(inputCar.getSerial());
			if(isExistSerial){
				result = WeChatResultCode.ERROR_2001.getValue();
				LOGGER.info("车辆编号已存在!");
				return result;
			}
			//查询车辆的车牌信息
			boolean isExistLicensePlate = car.isExistInMarketCarInfoBylicensePlate(inputCar.getLicensePlate());
			if(isExistLicensePlate){
				result = WeChatResultCode.ERROR_2002.getValue();
				LOGGER.info("车牌号码已存在!");
				return result;
			}*/
//			//查询车辆的过户流水号（车辆过户信息表中搜索）
//			boolean isExistTransferSerial= Tai.findTransInfobySerial(inputCar.getTransferSerial());
//			if(isExistTransferSerial){
//				result = WeChatResultCode.ERROR_3001.getValue();
//				LOGGER.info("过户流水号已存在!");
//				return result;
//			}
			//根据新车牌查询车牌信息
			MarketCarInfoDto carInfo = car.isExistInMarketCarInfoByNewLicensePlate(inputCar.getLicensePlate());
			if(carInfo != null){
				inputCar.setRegisterStatus(carInfo.getRegisterStatus());
				car.updateRegisterStatus(inputCar.getSerial(), inputCar.getRegisterStatus(), inputCar.getUpdateUserId());
			} else {
				inputCar.setRegisterStatus(6);	
			}
			inputCar.setCarName(car.findBrandNameById(inputCar.getBrandId()));
			
			TransferAcceptedInfoDto info = car.findTransfeAcceptedInfo(inputCar.getLicensePlate());
			if(info != null) {
				int updateUserId = 1;
				int marketType = 1;
				int status = 4;
				car.updateTransfeAcceptedInfo(info.getSerial(), marketType, inputCar.getSerial(), updateUserId);
				int oldStatus = car.getTransferPersonalCarInfo( inputCar.getSerial());
				if(oldStatus != 0 ) car.updateMarketCarInfo(info.getBasicCarId(), oldStatus, updateUserId);
				car.updateTransferPersonalCarInfo(info.getBasicCarId(), status, updateUserId);
			}
			
			//录入状态为 1：待录入
			inputCar.setEntryStatus(1);
			//过户状态为 1：正常
			inputCar.setStatus(1);
			
			//认证状态为 1：未认证
			inputCar.setAttestStatus(1);
			//车主姓名
			car.setCarOwner("");
			//插入用户
			inputCar.setInsertUserId(1);
			//更新用户
			inputCar.setUpdateUserId(1);
			//插入库存车辆信息表（tms_market_car_info）
			car.insertCarInfoWhthWeiChat(inputCar);
			
//			//插入“过户业务基本信息表 (tms_transfer_accepted_info)”
//			Tai.setLicensePlate(inputCar.getLicensePlate());
//			Tai.setBasicCarId(inputCar.getSerial());
//			Tai.setSerial(inputCar.getTransferSerial());
//			Tai.setMarketType(1);
//			Tai.setCarClass(2);
//			Tai.setStatus(0);
//			Tai.setSourceType(1);
//			Tai.setMemberId(String.valueOf(inputCar.getMemberId()));
//			Tai.setInsertUserid(1);
//			Tai.setUpdateUserId(1);
//			Tai.insertCarWithWeiChat(Tai);
			
////		插入”过户流程操作记录表（tms_transfer_process_history）“
//			TransferProcessHistory transferProcessHistory = new TransferProcessHistory();
//			transferProcessHistory.setSerial(inputCar.getTransferSerial());
//			transferProcessHistory.setBusinessType(0);
//			transferProcessHistory.setInsertUserId(1);
//			transferProcessHistory.insertHisInfoAtAssess(transferProcessHistory);
//			
//			//插入”tms_transfer_finance_info（过户业务的财务收费信息表）“
//			TransferFinanceInfo transferFinanceInfo = new TransferFinanceInfo();
//			transferFinanceInfo.insertFinanceByInner(inputCar.getTransferSerial());
			
			tx.commit();
		
		} catch (Exception e) {
			LOGGER.error("AbstractTmsCarWeChatService error,", e);
			result = WeChatResultCode.ERROR_1002.getValue();
			try {
				tx.rollback();
			} catch (TransactionException ex) {
				LOGGER.error("rollback error,", ex);
			}
		} 
		
		return result;
	}
	
	/**
	 * @param TransferAcceptedInfo
	 * @param int
	 * @return int
	 */
	public int transferApply(TransferAcceptedInfo accept,int memberType) {
		
		int result = WeChatResultCode.SUCCESS.getValue();
		TxController tx = TxController.getTxController();
		try {
			tx.begin();
			//1）过户流水号不存在check：如果当前车辆是库存车辆，使用过户流水号到过户业务基本信息表中搜索，如果不存在表明流水号不存在；
			if(accept.getMarketType()==1){
				TransferAcceptedInfo accept1 = accept.findTransStatusBySerial(accept.getSerial());
				if(accept1 == null){
					//进行失败2方式处理
					result = WeChatResultCode.ERROR_7001.getValue();
					LOGGER.info("过户信息不存在!");
					return result;
				}
			}
			
			//2）非1）情况，数据库操作具体如下：
			//a）如果当前车辆是库存车辆的情况:
			if(accept.getMarketType()==1){
				//更新 过户业务基本信息表（tms_transfer_accepted_info）
				TransferAcceptedInfo acceptData = accept.getAcceptInfoBySerial(accept.getSerial(),"");
				acceptData.setStatus(1);
				acceptData.setCy_cardType(accept.getCy_cardType());
				acceptData.setPhoto_1(accept.getPhoto_1());
				//如果会员类别是经办人会员，那么更新经办人ID（attnId）为传入会员ID
				if(memberType==2){
					acceptData.setAttnId(accept.getMemberId());
				}
				acceptData.setUpdateUserId(1);
				accept.updateAcceptInfoByWeiChart(acceptData);
				
				//车辆过户状态（status）为“3：过户中”
				Car car = new Car();
				car = car.findCarBySerial(accept.getBasicCarId());
				car.setUpdateUserId(1);
				car.setStatus(3);
				car.updateMarketInfoByLicensePlate(car);
				//插入 过户流程操作记录表（tms_transfer_process_history）
				TransferProcessHistory history = new TransferProcessHistory();
				history.setSerial(accept.getSerial());
				history.setBusinessType(1);
				history.setInsertUserId(1);
				history.setReason("");
				history.insertHisInfoAtAssess(history);
			} else {
				//b）如果当前车辆不是库存车辆的情况:
				//插入 ”非库存车辆信息表（tms_transfer_personalCar_info）“
				TransferPersonalCarInfo personal = new TransferPersonalCarInfo();
				//非库存表
				personal.setSerial(accept.getBasicCarId());
				personal.setStatus(3);
				personal.setUpdateUserId(1);
				personal.setLicensePlate(accept.getLicensePlate());
				personal.insertCarBaseInfoAtAssess(personal);
				
				//插入“过户业务基本信息表 (tms_transfer_accepted_info)”
				TransferAcceptedInfo newAccept = new TransferAcceptedInfo();
				newAccept.setSerial(accept.getSerial());
				newAccept.setBasicCarId(accept.getBasicCarId());
				newAccept.setLicensePlate(accept.getLicensePlate());
				newAccept.setCy_cardType(accept.getCy_cardType());
				newAccept.setPhoto_1(accept.getPhoto_1());
				newAccept.setCarClass(2);
				newAccept.setMarketType(2);
				newAccept.setStatus(1);
				newAccept.setInsertUserid(1);
				newAccept.setUpdateUserId(1);
				//如果会员类别是经办人会员，那么更新经办人ID（attnId）为传入会员ID，如果会员类别是商户会员，那么就更新会员ID（memberId）为传入会员ID
				if(memberType==2){
					newAccept.setAttnId(accept.getMemberId());
				}else{
					newAccept.setMemberId(accept.getMemberId());
				}
				accept.insertCarWithWeiChat(newAccept);
				//插入”过户流程操作记录表（tms_transfer_process_history）“
				TransferProcessHistory transferProcessHistory = new TransferProcessHistory();
				transferProcessHistory.setSerial(accept.getSerial());
				transferProcessHistory.setBusinessType(1);
				transferProcessHistory.setInsertUserId(1);
				transferProcessHistory.insertHisInfoAtAssess(transferProcessHistory);
				
				//插入”tms_transfer_finance_info（过户业务的财务收费信息表）“
				TransferFinanceInfo transferFinanceInfo = new TransferFinanceInfo();
				transferFinanceInfo.insertFinanceByInner(accept.getSerial());
				
				tx.commit();
			}
		} catch (Exception e) {
			LOGGER.error("AbstractTmsCarWeChatService error,", e);
			result = WeChatResultCode.ERROR_1002.getValue();
			try {
				tx.rollback();
			} catch (TransactionException ex) {
				LOGGER.error("rollback error,", ex);
			}
		} 
		
		return result;
		
	}
	
}
